package com.sgb.kjwl.viewmodel.home;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson3;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.home.MaterialHomeFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MaterialHomeModel extends BaseViewModel {

    MaterialHomeFragment mMaterialHomeFragment;
    public ObservableField<MaterialFilterJson3> materialFilterJson = new ObservableField<>(new MaterialFilterJson3());
    private HomeRepertory repertory;
    private Map map;


    public MaterialHomeModel(MaterialHomeFragment fragment, HomeRepertory repertory) {
        this.mMaterialHomeFragment = fragment;
        this.repertory = repertory;
    }

    public void getMenuModel(String areacode, String id) {
        if (areacode.equals("")) {
            areacode = repertory.getAreaCode();
        }
        if (map == null) {
            map = new HashMap();
        }
        map.put("industryId", id);
        map.put("areaCode", areacode);
        APIManager.getInstance().getMaterialAPI().getItemCatList(map).enqueue(new Callback<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MenuEntity>>> call, Response<BaseEntity<List<MenuEntity>>> response) {
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mMaterialHomeFragment.showMenuView(response.body().getData());
                } else {
                    MToast.showToast(mMaterialHomeFragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MenuEntity>>> call, Throwable t) {
                MToast.showToast(mMaterialHomeFragment.getActivity(), t.getMessage());
            }
        });
    }

    public void recommendMenu() {
        APIManager.getInstance().getMaterialAPI().recommendMenuNew().enqueue(new Callback<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MenuEntity>>> call, Response<BaseEntity<List<MenuEntity>>> response) {
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mMaterialHomeFragment.showImageButton(response.body().getData());
                } else {
                    MToast.showToast(mMaterialHomeFragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MenuEntity>>> call, Throwable t) {
                MToast.showToast(mMaterialHomeFragment.getActivity(), t.getMessage());
            }
        });
    }

    public void getIndustryList() {
        DialogHelper.showProgressDialog(mMaterialHomeFragment.getActivity(), null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMaterialAPI().getIndustryList().enqueue(new Callback<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MenuEntity>>> call, Response<BaseEntity<List<MenuEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mMaterialHomeFragment.showIndustry(response.body().getData());
                } else {
                    MToast.showToast(mMaterialHomeFragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MenuEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mMaterialHomeFragment.getActivity(), t.getMessage());
            }
        });
    }

    public void saveProvinceModel(String name, String code) {
        repertory.setAreaCode(code);
        repertory.setAreaName(name);
    }
}
